package sealte.hvpms.servlet.vp;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sealte.hvpms.dao.PollOptionDAO;
import sealte.hvpms.dao.impl.PollOptionDAOImpl;
import sealte.hvpms.pojo.UserCreate;

import com.google.gson.Gson;

public class BatchVPsBeZero extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/html");
		response.setHeader("Cache-Control","no-cache");
		UserCreate uc = (UserCreate)request.getSession().getAttribute("UserCreate");
		PrintWriter out = response.getWriter();
		if(uc!=null){
			String polls = (String)request.getParameter("polls");
			if(polls!=null&&!polls.equals("")){
				String[] poll_idStrs = polls.split("[,]");
				int[] poll_ids = new int[poll_idStrs.length];
				for(int i=0;i<poll_idStrs.length;i++){
					poll_ids[i] = Integer.parseInt(poll_idStrs[i]);
				}
				String message = "";
				PollOptionDAO pob = new PollOptionDAOImpl();
				boolean flag = pob.batchVPsBeZero(poll_ids);
				if(flag){
					message = "ok";
				}else{
					message = "fail";
				}
				Gson gson = new Gson();
				String json = gson.toJson(message);
				response.getWriter().write(json);
			}else{
				out.println("系统出错，请联系开发者");
			}
		}else{
			out.println("谢绝跨路径访问");
		}
	}

}
